package edu.xmist.demo15.controller;

import edu.xmist.demo15.core.Statistics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

public class TFactory {

    private static final Map<String, List<Statistics>> CACHE = new HashMap<>();
    private static final String statisticsType = "test";

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(getInstance());
        }
    }

    public static Statistics getInstance() {
        switch (statisticsType) {
            //..
            case "test":
                return getTestStatisticsInstance(statisticsType);
            default:
                return null;
        }
    }

    private static Statistics getTestStatisticsInstance(String key) {
        List<Statistics> list = CACHE.get(key);
        if (list == null) {
            list = new ArrayList<>();
            list.add(new TestStatistics());
            CACHE.put(key, list);
            return list.get(0);
        }
        if (list.size() == 1) {
            list.add(new TestStatistics());
        }
        return list.get(ThreadLocalRandom.current().nextInt(2));
    }

    static class TestStatistics implements Statistics {

        @Override
        public int incr() {
            return 0;
        }
    }
}
